<?php

class Thangthanh extends Controller {

	function Thangthanh()
	{
		parent::Controller();
		$this->load->helper('url');
		$this->load->model('model_cp','model');
		$this->load->library('session');
	}
	function vi(){
		$this->load->library('session');
		$this->session->set_userdata('lang',0);
		redirect();
	}

	function en(){
		$this->load->library('session');
		$this->session->set_userdata('lang',1);
		redirect();
	}
	
	function _data(){
		$data = array();
		if($this->session->userdata('lang')==1){
			$lang = 'en';
			$textlang = "english";
		}else{
			$lang = 'vi';
			$textlang = "vietnam";
		}
		$this->lang->load($lang, $textlang);
		
		$data['text_hoten']       = $this->lang->line('text_hoten');
		$data['text_dienthoai']       = $this->lang->line('text_dienthoai');
		$data['text_diachi']       = $this->lang->line('text_diachi');
		$data['text_noidung']       = $this->lang->line('text_noidung');
		$data['text_tieude']       = $this->lang->line('text_tieude');
		
		/////////////Data
		$data['topmenu'] = $this->model->get_topmenu();
		$data['danhmucsanpham'] = $this->model->get_danhmucsanpham(0);//danh muc
		$data['sanphamhot'] = $this->model->sanphamhot(0, 0, 0);//danhmuc, limit, offset
		$data['baiviettrangchu'] = $this->model->get_baiviet_trangchu(0, 6);//danhmuc, limit
		$data['slide'] = $this->model->get_slide();
		$data['soluottruycap'] = $this->get_counter();
		$data['hinhanh_home'] = $this->model->get_hinhanh();
		//$data['sanpham_home'] = $this->model->get_sanpham_trangchu(0, 9, 0);//danhmuc, limit, offset
		//////////////////
		$this->activewebsite();
		return $data;
	}
	
	function activewebsite(){
		if($this->model->get_trangthaiwebsite()->activewebsite==0)
		{
			$data['message'] = $this->model->get_trangthaiwebsite()->noidungtatwebsite;
			$this->load->view('view_tatwebsite',$data);
		}
	}
	function counter(){
		$this->db->insert('tblcounter', array('luottruycap'=>1));
	}
	function get_counter(){
		$this->db->select_max('id');
		$query = $this->db->get('tblcounter');
		return $query->row();
	}
	
	function index($offset=0)
	{
		$data = $this->_data();
	
		$this->set_sesion_menu('1');
///		echo $this->session->userdata('active_menu');
		$this->load->library('pagination');
		$limit = 9;
		$data['trangchu']=1;
		$config['base_url'] = site_url().'/thangthanh/index/';
		$config['total_rows'] = $this->model->get_count_sanpham_trangchu(0);//danh muc
		$config['per_page'] = $limit;
		$config['uri_segment'] = 3;
		$this->pagination->initialize($config); 
		$data['sanphamtrangchu'] = $this->model->get_sanpham_trangchu(0, $limit, $offset);//danhmuc, limit, offset
		$this->counter();
		
		$this->load->view('view_template',$data);
		
		
	}
	function set_sesion_menu($giatri='trangchu'){
		$this->session->set_userdata('active_menu',$giatri);
	}
	
	function lienhe(){
		$data = $this->_data();
		$this->set_sesion_menu(5);
		if($this->input->post('btnLienHe')){
			$hoten = $this->input->post('txtHoTen');
			$email = $this->input->post('txtEmail');
			$dienthoai = $this->input->post('txtDienThoai');
			$diachi = $this->input->post('txtDiaChi');
			$noidung= $this->input->post('txtNoiDung');
			$message = "<strong>Họ tên: </strong>$hoten<br/>
						<strong>Email: </strong>$email<br/>
						<strong>Điện thoại: </strong>$dienthoai<br/>
						<strong>Địa chỉ: </strong>$diachi<br/>
						<strong>Nội dung</strong><br/>
						$noidung";
			$emailhethong = 'chicuong.website@gmail.com';//strip_tags($this->model->get_config('emailhethong')->giatri);
			$taikhoan_gmail = '';
			$pass_gmail='';
			$tieude = 'CTy TNHH Thắng Thanh: Liên hệ từ khách hàng';
			$this->guimail($email, $hoten, $tieude, $noidung);
			
			$data['message']='Gửi nội dung thành công. Chúng tôi sẽ liên lạc với bạn trong thời gian sớm nhất.';
		}
		
		$data['subview']='view_lienhe';
		$this->load->view('view_template', $data);
		
	}
	
	function sendEmail($mailgui, $mailnhan, $noidung, $taikhoan_gmail, $pass_gmail, $hoten='',$tieude='Lien he')
	{
		//config email here
		$config = array(
			'protocol' => 'smtp',
			'smtp_host' => 'ssl://smtp.googlemail.com',
			'smtp_port' => 465,
			'smtp_user' => "$taikhoan_gmail",
			'smtp_pass' => "$pass_gmail",
		);
		
		$this->load->library('email', $config);
		$this->email->set_newline("\r\n");

		$this->email->from("$mailgui", "$hoten");
		$this->email->to("$mailnhan"); 
		
		//$this->email->cc('another@another-example.com'); 
		//$this->email->bcc('them@their-example.com'); 
		
		$this->email->subject("$tieude");
		$this->email->message($noidung);	
		if($this->email->send())
			return true;
		else
			return false;
	}
	function thoitiet($id){
		
			$Link = array();
			$Link[5] = 'http://vnexpress.net/ListFile/Weather/Sonla.xml';
			$Link[2] = 'http://vnexpress.net/ListFile/Weather/Haiphong.xml';
			$Link[6] = 'http://vnexpress.net/ListFile/Weather/Hanoi.xml';
			$Link[8] = 'http://vnexpress.net/ListFile/Weather/Vinh.xml';
			$Link[1] = 'http://vnexpress.net/ListFile/Weather/Danang.xml';
			$Link[3] = 'http://vnexpress.net/ListFile/Weather/Nhatrang.xml';
			$Link[4] = 'http://vnexpress.net/ListFile/Weather/Pleicu.xml';
			$Link[7] = 'http://vnexpress.net/ListFile/Weather/hcm.xml';
			$id= $id;//isset($_GET['id'])?intval($_GET['id']):0;
//			echo $id;
			$content = file_get_contents($Link[$id]);
			$p = xml_parser_create();
			xml_parse_into_struct($p, $content, $xml);
			xml_parser_free($p);
			
			//echo $xml[5]['value'];
			//echo $Link[$id];
			$img='<img src="http://vnexpress.net/Images/Weather/'.$xml[3]['value'].'" align="left" />';
			
			if(isset($xml[5]['value'])){
				$img.='<img src="http://vnexpress.net/Images/Weather/'.$xml[5]['value'].'" align="left" />';
			}
//			echo $img;
			echo '<br/>
			<img src="http://vnexpress.net/Images/Weather/'.$xml[1]['value'].'" align="left" />
			
			'.$img.'
			<img src="http://vnexpress.net/Images/Weather/c.gif" align="left" />
			<br/><br/><p>';
			
			echo $xml[13]['value'].'</p>';
	}
	function guimail($email, $hoten, $tieude, $noidung){
		require("class.phpmailer.php");
		$mail = new PHPMailer();
		//********************************************/
		$SMTP_Host = "website.com";
		$SMTP_Port = 25;
		$SMTP_UserName = "info@website.com";
		$SMTP_Password = "274109";
		$from = $SMTP_UserName;
		$to = "chicuong@gmail.com";
		// Luu y: $SMTP_UserName = $from
		//********************************************/
		
		$mail->IsSMTP();
		$mail->Host     = $SMTP_Host;
		$mail->SMTPAuth = true;
		$mail->Username = $SMTP_UserName;
		$mail->Password = $SMTP_Password;
		
		$mail->From     = $from;
		$mail->FromName = "$hoten";
		$mail->AddAddress($to);
		//$mail->AddReplyTo("info@site.com","Information");
		
		$mail->WordWrap = 50;
		//$mail->AddAttachment("/var/tmp/file.tar.gz");
		//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
		$mail->IsHTML(true);
		
		$mail->Subject  =  "$tieude";
		$mail->Body     =  "$noidung";
		$mail->AltBody  =  "This is the text-only body";
		
		if(!$mail->Send())
		{
		   echo "Mail gui khong thanh cong! <p>";
		   echo "Thong bao loi: " . $mail->ErrorInfo;
		   exit;
		}
///		echo "Mail gui thanh cong!";

	}
	function listtin($danhmuc=0, $offset=0){
		$data = $this->_data();
		if($this->model->get_count_baiviet($danhmuc)==1){
			redirect('thangthanh/detail/'.$this->model->get_baiviet_dautien_of_danhmuc($danhmuc)->id);
		}
		$this->load->library('pagination');
		$this->set_sesion_menu($danhmuc);
		$limit = 6;
		$config['base_url'] = site_url().'/thangthanh/listtin/'.$danhmuc.'/';
		$config['total_rows'] = $this->model->get_count_baiviet($danhmuc);//danh muc
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$this->pagination->initialize($config); 
		$data['danhsachbaiviet'] = $this->model->get_baiviet($danhmuc, $limit, $offset);
		$data['title'] = $this->model->chitiet_topmenu($danhmuc)->tieude;
		$data['url'] = 'detail';
		$data['subview'] = 'view_listbaiviet';
		$this->load->view('view_template', $data);
	}
	function detail($id){
		$data = $this->_data();
		$data['subview'] = 'view_detail';
		$data['chitiettin'] = $this->model->chitietbaiviet($id);
		$this->set_sesion_menu($data['chitiettin']->danhmuc);
		$data['tinkhac'] = $this->model->get_tinkhac_baiviet($data['chitiettin']->danhmuc, $id, 6);
		$data['title'] = $this->model->chitiet_topmenu($data['chitiettin']->danhmuc)->tieude;
		$data['url'] = 'detail';
		$this->load->view('view_template', $data);
	}
	function listproduct($danhmuc=0, $offset=0){
		$data = $this->_data();
		$this->load->library('pagination');
		$limit = 12;
		$config['base_url'] = site_url().'/thangthanh/listproduct/'.$danhmuc.'/';
		$config['total_rows'] = $this->model->get_count_allsanpham($danhmuc);
		$config['per_page'] = $limit;
		$config['uri_segment'] = 4;
		$this->pagination->initialize($config); 
		$data['sanphamtrangchu'] = $this->model->get_all_sanpham($danhmuc, $limit, $offset);
		if($danhmuc>0)
			$data['title'] = $this->model->chitietdanhmucsanpham($danhmuc)->tieude;
		else
			$data['title'] = 'Sản phẩm';
		$this->set_sesion_menu(1);
		$data['url'] = 'detailproduct';
		$data['subview'] = 'view_listsanpham_home';
		$this->load->view('view_template', $data);
	}
	function detailproduct($id){
		$data = $this->_data();
		$data['subview'] = 'view_detailproduct';
		$data['chitietsanpham'] = $this->model->chitietsanpham($id);
		$data['sanphamkhac'] = $this->model->sanphamkhac($data['chitietsanpham']->danhmuc, $id, 6);
		$data['url'] = 'detailproduct';
		$data['title'] = 'Thông tin sản phẩm';
		$this->load->library('pagination');
		$this->set_sesion_menu(1);
		$this->load->view('view_template', $data);
	}
	function search(){
		//if(!$this->input->post('btnSearch'))
			//redirect();
			$data = $this->_data();
		$tukhoa_temp = trim($this->input->post('txtTuKhoa'));
		$this->load->library('pagination');
		$tukhoa = str_replace("'", "",$tukhoa_temp);
		//echo $tukhoa;
		$this->set_sesion_menu(1);
		$data['subview'] = 'view_listsanpham_home';
		$data['sanphamtrangchu'] = $this->model->search($tukhoa);
		$data['title'] = 'Kết quả tìm kiếm';
		$this->load->view('view_template', $data);
	}
	
}